<template>
  <div class="BatchCollect">
    <el-dialog
      title="批量设置业务员" :center="true" width="700px" :visible.sync="dialogFormVisible">
      <el-form :model="dataForm" ref="dataForm" :rules="roleRules">
        <el-form-item label="开发业务员：" prop="developUserId" label-width="180px">
          <el-select
            v-model="dataForm.developUserId"
            filterable clearable
            placeholder="请选择开发业务员">
            <el-option
              v-for="item in pickUserList"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="维护业务员：" prop="keepUserId" label-width="180px">
          <el-select
            v-model="dataForm.keepUserId"
            filterable clearable
            placeholder="请选择维护业务员">
            <el-option
              v-for="item in pickUserList"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="合作折扣：" prop="discount" label-width="180px">
          <el-input v-model="dataForm.discount" v-maxnum="{ obj: dataForm, key: 'discount', max: 100 }" placeholder="可输入1-100之间" clearable>
            <template slot="suffix"> % </template>
          </el-input>
        </el-form-item>

        <el-form-item label="* 合作协议：" label-width="180px">
          <upload-oss folder="institution" accept="image,pdf"
                      @uploadFileList="getUploadFileList"></upload-oss>
        </el-form-item>
        <el-form-item align="center">
          <el-button type="primary" plain @click.native="clickCommit">确 定</el-button>
          <el-button @click.native="clickCancel">取 消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import AuthApi from "@/api/system/auth";
import InstitutionApi from '@/api/institution/institution';
import uploadOss from '@/components/UploadOss';

export default {
  name: 'BatchSetBusinessMan',
  props: {
    relationType: {
      type: Number,
      default: null // 1.医检 2.医院 3.科室 4.医生
    },
  },
  components: {
    // 引入组件
    uploadOss
  },
  data() {
    return {
      pickUserList: [],
      dataForm: {
        contractImages: '',
        developUserId: '',
        keepUserId: '',
        discount: ''
      },
      selectIds: [],
      dialogFormVisible: false,
      roleRules: {
        developUserId: [
          {required: true, message: "请选择开发业务员", trigger: "blur"}
        ]
      }
    }
  },
  mounted() {
    this.listSyUser();
    this.$refs.uploadOss.handleCreate();
  },
  methods: {
    listSyUser() {
      let _this = this
      AuthApi.listAdminByRole({roleId:36})
        .then(res => {
          setTimeout(() => {
            _this.pickUserList = res.data.map(item => {
              return {
                value: item.userId,
                label: item.userName
              }
            })
          }, 200)
        })
    },
    clickCommit() {
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          this.addData();
        }
      })
    },
    addData() {
      // 待发送请求
      let _this = this
      if (!_this.dataForm.contractImages){
        _this.$Utils.messageError('请上传合作协议');
        return;
      }
      let cond = {
        ids: _this.selectIds,
        dataType: _this.relationType,
        contractImages: _this.dataForm.contractImages,
        developUserId: _this.dataForm.developUserId,
        keepUserId: _this.dataForm.keepUserId,
        discount: _this.dataForm.discount
      }
      InstitutionApi.batchSetBusinessMan(cond)
        .then(res => {
          _this.$Utils.notifySuccess();
          _this.clickCancel();
        })
    },
    clickCancel() {
      this.$Utils.clearData(this.dataForm);
      this.dialogFormVisible = false;
    },
    getUploadFileList(val) {
      let _this = this;
      if (!val) {
        _this.$Utils.messageError('请选择文件上传');
        return;
      }
      _this.dataForm.contractImages = val.join(",");
    },
    handleCreate(val) {
      // 父组件调动子组件方法
      this.selectIds = val;
      this.dialogFormVisible = true;
    }
  }
}
</script>

<style>

</style>
